<?php namespace Common\Tag;

use Hdphp\View\TagBase;

class Common extends TagBase
{
    /**
     * 标签声明
     * @var array
     */
    public $tags = array(
        'category' => array('block' => 1, 'level' => 4),
        'arclist' => array('block' => 1, 'level' => 4),
        'pagelist' => array('block' => 1, 'level' => 4),
        'pageshow' => array('block' => 0, 'level' => 4),
        'prenext' => array('block' => 0, 'level' => 4),
        );

    /**
     * 测试标签
     * @param $attr 属性
     * @param $content 内容
     * @param $hd HdView模型引擎对象
     */
    public function _category($attr, $content, &$hd)
    {
        $attr['id'] = isset($attr['id'])?$attr['id']:0;
        $php=<<<str
        <?php
        \$db = Db::table("category");

        switch('{$attr['type']}')
        {
            case 'top':
                //顶级
            \$data = \$db->where('pid',0)->get();
            break;
            case 'self':
                //同级
            \$pid = \$db->where('id',{$attr['id']})->pluck('pid');
            \$data = \$db->where('pid',\$pid)->get();
            break;
            case 'son':
            \$data = \$db->where('pid',{$attr['id']})->get();
            break;
        }
        foreach(\$data as \$field):
			if(\$field['type']==1)
			{
            \$field['url']="list_{\$field['id']}.html";
            }
			else
			{
				\$field['url']=__ROOT__.'/h/'.\$field['dir'];
			}
            ?>
        {$content}
        <?php endforeach;
    ?>
str;
        return $php;
    }

    public function _arclist($attr, $content, &$hd)
    {
        $titlelen = isset($attr['titlelen'])?$attr['titlelen']:20;
        $row = isset($attr['row'])?$attr['row']:10;
        $cid = isset($attr['cid'])?$attr['cid']:0;

$php =<<<str
    <?php
        \$cid = "$cid";
        \$db = Db::table('article');
        if(\$cid)
        {
            \$data = \$db->whereIn('cid',explode(',',\$cid))->limit($row)->get();
        }
        else
        {
            \$data = \$db->limit($row)->get();
        }

        foreach(\$data as \$field):
            \$field['title']=mb_substr(\$field['title'], 0,$titlelen,'utf8');
            \$field['url']=U('Home/Index/article',array('id'=>\$field['id']));
        ?>
        $content
        <?php 
        endforeach;
        ?>
str;
        return $php;
    }

    //栏目页分页数据展示
    public function _pagelist($attr,$content,&$hd)
    {
        //每页显示条数
        $row = isset($attr['row'])?$attr['row']:10;
       
        $php=<<<str
        <?php
        \$db = Db::table('article');
        \$count = \$db->where("cid",{$_GET['id']})->count('*');
		
		//设置分页的url
		\$cat = Db::table('category')->where('id',\$_GET['id'])->first();
		\$pageurl = __ROOT__.'/h/'.str_replace('{dir}',\$cat['dir'], \$cat['cathtml']);
		Page::url(\$pageurl);
        Page::row($row)->make(\$count);
        
        \$limit =  Page::limit();

        \$data = \$db->where('cid',{$_GET['id']})->limit(\$limit)->get();

        foreach(\$data as \$field):
            \$field['url']=U('Home/Index/article',array('id'=>\$field['id']));;
            ?>
           $content
        <?php endforeach;?>
str;
        return $php;
    }

    public function _pageshow($attr,$content,&$hd)
    {
        $php=<<<str
        <?php 
        \$db = Db::table('article');

        \$count = \$db->where("cid",{$_GET['id']})->count('*');

        echo Page::row({$attr['row']})->make(\$count);
        ?>
str;
        return $php;
    }

    //上一篇与下-篇
    public function _prenext($attr,$content,&$hd)
    {
        $php=<<<str
        <?php
 \$id= (int)\$_GET['id'];//5   6789
        \$pre = Db::table('article')->where("id",'<',\$id)->orderBy('id','DESC')->first();

        \$next = Db::table('article')->where("id",'>',\$id)->orderBy('id','ASC')->first();
        if(\$pre)
        {
          \$preLink = "<li><a href='".U('Home/Index/article',array('id'=>\$pre['id']))."'>".\$pre['title']."</a></li>";   
        }
        else{
            \$preLink="没有了";
        }

        if(\$next)
        {
          \$nextLink = "<li><a href='".U('Home/Index/article',array('id'=>\$next['id']))."'>".\$next['title']."</a></li>";   
        }
        else{
            \$nextLink="没有了";
        }
        echo \$preLink.\$nextLink;
        ?>
str;
       

       return $php;
    }
}




















